package com.woniuxy.thred.cooperation;

/**
 * description: CoTest01 <br>
 * date: 2022/2/6 2:48 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 协助模型：生产者消费者实现方式二：信号灯
 * 借助标志位
 */
public class CoTest02 {
    public static void main(String[] args) {
        Tv tv = new Tv();
        new Player(tv).start();
        new Watcher(tv).start();
    }
}

//生产者 演员
class Player extends Thread {
    Tv tv;

    public Player(Tv tv) {
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (i % 2 == 0) {
                this.tv.play("奇葩说");
            } else {
                this.tv.play("太污了，喝点可乐压压惊！");
            }
        }
    }
}

//消费者 观众
class Watcher extends Thread {
    Tv tv;

    public Watcher(Tv tv) {
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            tv.watch();
        }
    }
}

//同一个资源 电视
class Tv {
    //声音
    String voice;
    //信号灯
    /**
     * 如果为真，表示演员表演 观众等待
     * 如果为假，表示观众观看 演员等待
     */
    boolean flag = true;

    //表演
    public synchronized void play(String voice) {
        //演员等待
        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //表演时刻
        System.out.println("表演了：" + voice);
        this.voice = voice;
        //唤醒
        this.notifyAll();
        //切换标志
        this.flag = !this.flag;
    }

    //观看
    public synchronized void watch() {
        //观众等待
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //观看
        System.out.println("听到了：" + voice);
        //唤醒
        this.notifyAll();
        //切换标志
        this.flag = !this.flag;
    }
}